<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<title>Example 12Alt- (Capturing click events and translating them to matrix coordinates + Matrix Rotation)</title>
		
		<script>	

			window.onload = function () {
				var grid = {
					width: 5,
					height: 5
				}

				var canvas = document.getElementById('myCanvas');
				var c = canvas.getContext('2d');

				var tileMap = [];

				var tile = new Image();
				tile.src = "../img/tile.png";

				var dirt = new Image();
				dirt.src = "../img/dirt.png";

				canvas.addEventListener('mousedown', handleMouseDown, false);
				window.addEventListener('keydown', handleKeyDown, false);

				draw();

				// There's probably a more efficient way to do this
				function rotateMatrix(matrix, mW, mH, sW, sH) {
				    var m = [];
				    var sW = (sW === undefined) ? 0 : sW;
				    var sH = (sH === undefined) ? 0 : sH;

				    for (var i = sW; i < mW; i++) {
				        for (var j = sH; j < mH; j++) {
				        	var row = (mW - j) - 1;

				        	if (tileMap[row] !== undefined && tileMap[row][i]) {
				        		m[i] = (m[i] === undefined) ? [] : m[i];
				        		m[i][j] = tileMap[row][i];
				        	}
				        }
				    }

				    return m;
				}

				function handleKeyDown(e) {
					// Rotate the matrix when the user presses the R key
					if (e.keyCode === 82) {
						tileMap = rotateMatrix(tileMap, grid.width, grid.height);
					}

					draw();
				}

				function handleMouseDown(e) {
					var gridOffsetY = 0;
					var gridOffsetX = 0;

					// Take into account the offset on the X axis caused by centering the grid horizontally
					gridOffsetX += (canvas.width / 2) - (tile.width / 2);

					var col = (e.clientY - gridOffsetY) * 2;
					col = ((gridOffsetX + col) - e.clientX) / 2;
					
					var row = ((e.clientX + col) - tile.height) - gridOffsetX;

					row = Math.round(row / tile.height);
					col = Math.round(col / tile.height);

					// Check the boundaries!
					if (row >= 0 && 
						col >= 0 && 
						row <= grid.width &&
						col <= grid.height) {

						tileMap[row] = (tileMap[row] === undefined) ? [] : tileMap[row];
							
						tileMap[row][col] = 1;
					}

					draw();
				}

				
				function draw() {
					
					c.clearRect (0, 0, canvas.width, canvas.height);

					for (var row = 0; row < grid.width; row++) {
						for (var col = 0; col < grid.height; col++) {

							var tilePositionX = (row - col) * tile.height;

							// Center the grid horizontally
							tilePositionX += (canvas.width / 2) - (tile.width / 2);

							var tilePositionY = (row + col) * (tile.height / 2);

							if (tileMap[row] != null && tileMap[row][col] != null) {
								c.drawImage(dirt, Math.round(tilePositionX), Math.round(tilePositionY), dirt.width, dirt.height);
							} else {
								c.drawImage(tile, Math.round(tilePositionX), Math.round(tilePositionY), tile.width, tile.height);	
							}
						}	
					}
				}
			}
		</script>
    </head>
    <body>
		<canvas id="myCanvas" width="600" height="300"></canvas><br />
		Press R to rotate the matrix
    </body>
</html>